package dao;

import entity.Goods;
import utils.JDBCHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class GoodDao implements BaseDao<Goods> {
    private static final  String TABLENAME = "t_goods" ;

    @Override
    public boolean add(Goods o) {
        return false;
    }

    @Override
    public boolean delete(Goods o) {
        return false;
    }

    @Override
    public boolean update(Goods o) {
        return false;
    }

    @Override
    public List<Goods> loadAll() {
        return null;
    }
    /**
     * 根据用户名查询用户信息
     * @param gname 用户名
     * @return 返回一个用户对象
     */
    public Goods loadByRealName (String gname){
        Goods good = null;
        //利用DBHelper 查询数据库
        String sql = "select gid , gname , price , type , g_picture from " + TABLENAME+" where gname=?" ;
        ResultSet rs = JDBCHelper.executeQuery(sql , gname ) ;
        good = wrap(rs) ;
        return good ;
    }

    public Goods loadByGid(String gid){
        Goods good = null ;
        //利用DBHelper 查询数据库
        String sql = "select gid , gname , price , type , g_picture from " + TABLENAME+" where gid=?" ;
        ResultSet rs = JDBCHelper.executeQuery(sql , gid ) ;
        good = wrap(rs) ;
        return good ;
    }

    /**
     * 包装数据到一个对象中
     * @param rs 结果集
     * @return 返回一个用户对象
     */
    public Goods wrap (ResultSet rs) {
        Goods good = null ;
        try {
            if(rs.next()){
                good = new Goods() ;
                good.setGid( rs.getString("gid"));
                good.setGname( rs.getString("gname"));
                good.setPrice(rs.getDouble("price"));
                good.setgPicture(rs.getString("g_picture"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return good ;
    }
}
